package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessRecordTemplateInfoDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.ItemProcessRecordTemplateInfo;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.InfoTemplate;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.InfoTemplateVisibilityEnum;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.ItemProcessRecordTemplateInfoService;
import com.jyu.apts.service.InfoTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationItemProcessRecordTemplateInfoController {

	@Autowired
	InfoTemplateService infoTemplateService;
	@Autowired
	UserService userService;
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	@Autowired
	ItemProcessRecordTemplateInfoService itemProcessRecordTemplateInfoService;
	
	@RequiresPermissions("organizationItemProcessRecordTemplateInfo_html")
	@RequestMapping("/organizationItemProcessRecordTemplateInfo_html")
	public ModelAndView organizationItemProcessRecordTemplateInfo_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationItemProcessRecordTemplateInfo.html");
		return mv;
	}
	
	@RequiresPermissions("organizationItemProcessRecordTemplateInfo_html")
	@RequestMapping("/addOrganizationItemProcessRecordTemplateInfo")
	public ResultDto addItemProcessRecordTemplateInfo(String[] dataValues,long infoTemplateId,long itemProcessRecordId) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		InfoTemplate infoTemplate = infoTemplateService.getOne(infoTemplateId);
		if(infoTemplate == null) {
			return new ResultDto("信息模板不存在",null,2);
		}
		if(infoTemplate.getVisibility()!=InfoTemplateVisibilityEnum.PUBLIC.getType() && infoTemplate.getAuthor().getOrganization().getId()!=organization.getId()) {
			return new ResultDto("没有权限使用该模板",null,2);
		}
		ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordId);
		if(itemProcessRecord.getItemBatchRecord().getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
			return new ResultDto("流程记录不存在",null,2);
		}
		ItemProcessRecordTemplateInfo organizationItemProcessRecordTemplateInfo = new ItemProcessRecordTemplateInfo();
		StringBuilder sb = new StringBuilder("");
		if(dataValues == null || dataValues.length == 0) {
			sb.append("#");
		}
		for(String dataValue : dataValues) {
			sb.append("#");
			sb.append(dataValue);
		}
		organizationItemProcessRecordTemplateInfo.setDataValues(sb.toString().substring(1));
		organizationItemProcessRecordTemplateInfo.setInfoTemplate(infoTemplate);
		organizationItemProcessRecordTemplateInfo.setItemProcessRecord(itemProcessRecord);
		itemProcessRecordTemplateInfoService.save(organizationItemProcessRecordTemplateInfo);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProcessRecordTemplateInfo_html")
	@RequestMapping("/delOrganizationItemProcessRecordTemplateInfo")
	public ResultDto delItemProcessRecordTemplateInfo(String ids) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : ids.split(",")) {
			l_ids.add(new Long(id));
		}
		List<ItemProcessRecordTemplateInfo> organizationItemProcessRecordTemplateInfos = itemProcessRecordTemplateInfoService.findAllById(l_ids);
		for(ItemProcessRecordTemplateInfo itemProcessRecordTemplateInfo : organizationItemProcessRecordTemplateInfos) {
			if(itemProcessRecordTemplateInfo.getItemProcessRecord().getItemBatchRecord().getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("非法操作",null,2);
			}
		}
		itemProcessRecordTemplateInfoService.delete(organizationItemProcessRecordTemplateInfos);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProcessRecordTemplateInfo_html")
	@RequestMapping("/getOrganizationItemProcessRecordTemplateInfoPage")
	public TableDto<ItemProcessRecordTemplateInfoDto> getItemProcessRecordTemplateInfoPage(Integer pageNumber,Integer pageSize,Integer status,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		Page<ItemProcessRecordTemplateInfo> pageResult = itemProcessRecordTemplateInfoService.getItemProcessRecordTemplateInfoPage(pageNumber, pageSize, search, organization.getId(), null);
		List<ItemProcessRecordTemplateInfo> organizationItemProcessRecordTemplateInfos = pageResult.getContent();
		List<ItemProcessRecordTemplateInfoDto> organizationItemProcessRecordTemplateInfoDtos = new ArrayList<ItemProcessRecordTemplateInfoDto>();
		organizationItemProcessRecordTemplateInfos.forEach(organizationItemProcessRecordTemplateInfo->organizationItemProcessRecordTemplateInfoDtos.add(new ItemProcessRecordTemplateInfoDto().convert(organizationItemProcessRecordTemplateInfo)));
		return new TableDto<ItemProcessRecordTemplateInfoDto>(organizationItemProcessRecordTemplateInfoDtos,pageResult.getTotalElements());
	}
	
}
